/*
 * SearchEngine.java
 *
 * Created on 6 March 2006, 14:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.similarities.AfterEffect;
import org.apache.lucene.search.similarities.AfterEffectB;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.similarities.BasicModel;
import org.apache.lucene.search.similarities.BasicModelBE;
import org.apache.lucene.search.similarities.DFRSimilarity;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Distribution;
import org.apache.lucene.search.similarities.DistributionLL;
import org.apache.lucene.search.similarities.IBSimilarity;
import org.apache.lucene.search.similarities.LMDirichletSimilarity;
import org.apache.lucene.search.similarities.Lambda;
import org.apache.lucene.search.similarities.LambdaDF;
import org.apache.lucene.search.similarities.Normalization;
import org.apache.lucene.search.similarities.NormalizationH1;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Search {
	public IndexSearcher searcher = null;
	private boolean VERBOSE = true;

	/** Creates a new instance of SearchEngine */

	public Search() throws IOException {
		FSDirectory idx = FSDirectory.open(new File("index-directory"));
		DirectoryReader ireader = DirectoryReader.open(idx);
		searcher = new IndexSearcher(ireader);

	}

	public ScoreDoc[] performSearch(String queryString, int noOfTopDocs, int option, double threshold)
			throws Exception {

		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		Query query = new QueryParser(Version.LUCENE_CURRENT, "content", analyzer).parse(queryString);
		
		Similarity similarity = null;
    	switch(option) {
		case 0:
			similarity = new DefaultSimilarity();
			break;
		case 1:
			Distribution d = new DistributionLL();
			Lambda l = new LambdaDF();
			Normalization n = new NormalizationH1();
			similarity = new IBSimilarity(d,l,n);
			break;
		case 2:
			similarity = new BM25Similarity();
			break;
		case 3:
			BasicModel bm = new BasicModelBE();
			AfterEffect a = new AfterEffectB();
			Normalization nl = new NormalizationH1();
			similarity = new DFRSimilarity(bm, a, nl);
			break;
    	}
		
    	searcher.setSimilarity(similarity);
		TopDocs topDocs = searcher.search(query, noOfTopDocs);
		
		//System.out.print(searcher.explain(query, noOfTopDocs));
		// System.out.println(topDocs);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		if (VERBOSE)
			System.out.println("Total hits in topDocs: " + topDocs.totalHits
					+ "\n");

		for (int i = 0; i < scoreDocs.length; i++) {
			Document doc = searcher.doc(scoreDocs[i].doc); // This retrieves the
		}
		
		
		if (threshold!=0&&scoreDocs[scoreDocs.length-1].score > threshold) {
			return performSearch(queryString, noOfTopDocs+5,option,threshold );
		}
		
		return scoreDocs;
	}

}
